<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
require 'check.php';
class group extends check{
	//管理组列表
	public function lis(){
		$this->group = spClass('m_group')->findAll(null, 'id ASC');
		$this->display('group_lis.tpl.php');
	}
	//添加管理组
	public function add(){
		$this->display('group_add.tpl.php');
	}
	public function doadd(){
		if(false != spClass('m_group')->spVerifier($this->spArgs()))	$this->error('数据验证错误！',spUrl('group','add'));
		if(false != spClass('m_group')->create($this->spArgs())){
			$this->success('添加管理组成功！',spUrl('group','lis'));
		}else{
			$this->error('添加管理组失败！',spUrl('group','add'));
		}
	}
	//更新管理组
	public function upd(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('group','lis'));
		$this->group = spClass('m_group')->find(array('id'=>$id));
		$this->display('group_upd.tpl.php');
	}
	public function doupd(){
	if(false != spClass('m_group')->spVerifier($this->spArgs()))	$this->error('数据验证错误！',spUrl('group','lis'));
		$group = $this->spArgs();
		if(false != spClass('m_group')->update(array('id'=>$group['id']),$group)){
			$this->success('更新管理组成功！',spUrl('group','lis'));
		}else{
			$this->error('更新管理组失败！',spUrl('group','lis'));
		}
	}
	//删除管理组
	public function del(){
	if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('group','lis'));
		if(false != spClass('m_admin')->find(array('gid'=>$id))){
			$this->error('该管理组下存在管理员禁止删除！',spUrl('group','lis'));
		}elseif(false != spClass('m_group')->delete(array('id'=>$id))){
			//删除菜单权限及缓存文件
			spClass('m_gmenu')->delete(array('group_id'=>$id));
			spClass('m_gaacl')->delete(array('group_id'=>$id));
			$aaclFile = APP_PATH.'/cache/group_'.$id.'.aacl';
			$menuFile = APP_PATH.'/cache/group_'.$id.'.menu';
			if(file_exists($aaclFile)) unlink($aaclFile);
			if(file_exists($menuFile)) unlink($menuFile);
			$this->success('删除管理组成功！',spUrl('group','lis'));
		}else{
			$this->error('删除管理组失败！',spUrl('group','lis'));
		}
	}
	//检测标识唯一性
	public function checkcode(){
		if(!$code = trim($this->spArgs('code'))) exit;
		if(false != spClass('m_group')->find(array('code'=>$code))){
			exit('1'); //标识已存在
		}
		exit('0');
	}
}